﻿#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QDir>
#include <QSettings>
#include <QKeyEvent>
#include <QDebug>
login::login(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::login),
    isFlag(false)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    ui->errorMsg_label->setVisible(false);

    ui->userPassword_lineEdit->setEchoMode(QLineEdit::Password);
    initLogin();
}

login::~login()
{
    delete ui;
}


void login::on_forgetPassword_pushButton_clicked()
{
    m_forgetpassword.exec();

}

void login::on_confirm_pushButton_clicked()
{
    QString dirPath = "./DependFile/conf/";
    QDir confdir(dirPath);
    if(!confdir.exists())
        confdir.mkdir(dirPath);
    QString confFile(dirPath + "IDconf.ini");
    QSettings iniSetting(confFile, QSettings::IniFormat);
    QString password = iniSetting.value("password").toString();
    QString userName = iniSetting.value("userName").toString();

    QString rememberConfFile(dirPath + "remberIDconf.ini");
    QSettings remIniSetting(confFile, QSettings::IniFormat);
    QString remPassword = iniSetting.value("password").toString();
    QString remUserName = iniSetting.value("userName").toString();

    if(ui->userName_lineEdit->text() != userName)
    {
        ui->errorMsg_label->setVisible(true);

        return;
    }

    if(ui->userPassword_lineEdit->text() != password)
    {
        ui->errorMsg_label->setVisible(true);

        return;
    }

    emit signalResult(1);
    if(ui->remberPasswoed_radioButton->isChecked())
    {
        QString confFile(dirPath + "remberIDconf.ini");
        QSettings iniSetting(confFile, QSettings::IniFormat);
        iniSetting.setValue("password",ui->userPassword_lineEdit->text());
        iniSetting.setValue("userName",ui->userName_lineEdit->text());
    }

    this->close();

}

bool login::isFunctionCall()
{
    return isFlag;
}

void login::on_userName_lineEdit_textChanged(const QString &arg1)
{
    Q_UNUSED(arg1)
    ui->errorMsg_label->setVisible(false);

}


void login::on_userPassword_lineEdit_textChanged(const QString &arg1)
{
    Q_UNUSED(arg1)
    ui->errorMsg_label->setVisible(false);

}

void login::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}

void login::on_close_Btn_clicked()
{
    this->close();
}

void login::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Escape:
        break;
    default:
        QDialog::keyPressEvent(event);
    }
}

void login::initLogin()
{
    QString dirPath = "";
    QString confFile("./DependFile/conf/IDconf.ini");

    QSettings iniSetting(confFile, QSettings::IniFormat);
    qDebug()<<iniSetting.contains("isRemember")<<"11"<<iniSetting.allKeys();
    bool isRemember = iniSetting.value("isRemember").toBool();


    if(isRemember)
    {
        ui->remberPasswoed_radioButton->setChecked(true);
        QString pass;
        QString password = iniSetting.value("password").toString();
        QString userName = iniSetting.value("userName").toString();
        ui->userName_lineEdit->setText(userName);
        ui->userPassword_lineEdit->setText(password);
    }
    else
        ui->remberPasswoed_radioButton->setChecked(false);
}






